﻿using System.Web.Security;
using System.Text;
using System.Security.Cryptography;
using System;
using System.IO;

namespace XSTDZ.ERP.Infrastructure
{
    public static class CryptographerLite
    {
        private static readonly string cryptographeKey = "yfctct5X";
        private static readonly byte[] rgbIV = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="plaintext"></param>
        /// <returns></returns>
        public static string Encrypt(string plaintext)
        {
            byte[] rgbKey = null;
            try
            {
                rgbKey = Encoding.UTF8.GetBytes(cryptographeKey.Substring(0, 8));
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                byte[] bytes = Encoding.UTF8.GetBytes(plaintext);
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                stream2.Write(bytes, 0, bytes.Length);
                stream2.FlushFinalBlock();
                return Convert.ToBase64String(stream.ToArray());
            }
            catch (Exception exception)
            {
                return ("error:" + exception.Message + "\r");
            }

        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="ciphertext"></param>
        /// <returns></returns>
        public static string Decrypt(string ciphertext)
        {
            byte[] rgbKey = null;
            byte[] buffer = new byte[ciphertext.Length];
            try
            {
                rgbKey = Encoding.UTF8.GetBytes(cryptographeKey.Substring(0, 8));
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                buffer = Convert.FromBase64String(ciphertext);
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                stream2.Write(buffer, 0, buffer.Length);
                stream2.FlushFinalBlock();
                Encoding encoding = new UTF8Encoding();
                return encoding.GetString(stream.ToArray());
            }
            catch (Exception exception)
            {
                return ("error:" + exception.Message + "\r");
            }
        }
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="plaintext"></param>
        /// <returns></returns>
        public static string MD5(string plaintext)
        {
            return CreateHash(plaintext, "MD5");
        }
        /// <summary>
        /// SHA1加密
        /// </summary>
        /// <param name="plaintext"></param>
        /// <returns></returns>
        public static string SHA1(string plaintext)
        {
            return CreateHash(plaintext, "SHA1");
        }

        private static string CreateHash(string plaintext, string hashFormat)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(plaintext, hashFormat);
        }

        public static bool CompareMD5(string plaintext, string hashedText)
        {
            return Compare(plaintext, hashedText, "MD5");
        }
        public static bool CompareSHA1(string plaintext, string hashedText)
        {
            return Compare(plaintext, hashedText, "SHA1");
        }

        private static bool Compare(string plaintext, string hashedText, string hashFormat)
        {
            return string.Equals(hashedText, CreateHash(plaintext, hashFormat));
        }

    }
}
